<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use App\Model\Admin\Req;


class ReqController extends Controller
{

    //控制器中间件
   //protected  $middleware=['\App\Http\Middleware\AdminLogin::class'=>[]];


    public  function __construct()
    {

      //  $this->middleware('admin.login');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	$reqs = DB::table('req')
            ->join('user','req.user_id','=','user.id')
            ->select('req.*','user.username','user.email','user.phone')
            ->get();
          //dd($reqs);
         return view('admin.req_list')->with('reqs',$reqs);
    }

    public function show($id)
    {
        /*$data1 = DB::table('user')
        ->join('orders','user.id', '=', 'orders.user_id')
        ->select('user.userusername','orders.status','orders.time')
        ->where('orders.id',$id)       
        ->first();*/
        $reqs = DB::table('reply')
        ->join('req','req.id','=','reply.req_id')
        ->join('user','user.id','=','req.user_id')
        ->where('req.id',$id)
        ->select('user.username','reply.*')
        ->first();
        return view('admin.req_show',['reqs'=>$reqs]);   
    }
    public function edit($id)
    {
        $req = DB::table('req')
        ->join('user','req.user_id','=','user.id')
        ->where('req.id',$id)
        ->select('req.content','req.time','user.username','user.email','user.phone')->first();
        return view('admin.req_rep',['req'=>$req,'id'=>$id]);
    }
    public function update(Request $request,$id)
    {
        $data['req_id']   =  $id;
        $data['content']  =  $request->rep;
        $data['time']     =  time();
        DB::table('reply')->insert($data);
        DB::table('req')->where('id',$id)->update(['status'=>1]);
        return redirect('/admin/req/'.$id);
        
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $res = DB::table('req')->where('id',$id)->delete();
        return $res;
    }
}
